{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time, re\n",
    "\n",
    "from bs4 import BeautifulSoup\n",
    "from django.test import TestCase\n",
    "import requests as sys_requests\n",
    "import pandas as pd\n",
    "# import matplotlib.finance as mpf\n",
    "# import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_sina_api(code, year, quarter):\n",
    "    url = \"http://money.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/%s.phtml?year=%s&jidu=%s\" % (\n",
    "        code, year, quarter)\n",
    "    response = sys_requests.get(url)\n",
    "    content = str(response.content, 'gbk')\n",
    "    return content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "content = test_sina_api(\"600000\", 2019, 2)\n",
    "soup = BeautifulSoup(content, \"html.parser\")\n",
    "shares_table = soup.find(id=\"FundHoldSharesTable\")\n",
    "if shares_table != None:\n",
    "    list_table = shares_table.find_all(\"tr\")\n",
    "    list_table.pop(0)\n",
    "    page_title = list_table.pop(0)\n",
    "    page_title = page_title.find_all(\"strong\")\n",
    "    [print(col_name.get_text(), end=\" \") for col_name in page_title]\n",
    "\n",
    "    print()\n",
    "    columns = [\"日期\", \"开盘价\", \"最高价\", \"收盘价\", \"最低价\", \"交易量\", \"交易金额\"]\n",
    "    list_date = []\n",
    "    list_open_value = []\n",
    "    list_close_value = []\n",
    "    list_low = []\n",
    "    list_high = []\n",
    "    list_volume_num = []\n",
    "    list_volume = []\n",
    "\n",
    "    for line in list_table:\n",
    "        td = line.find_all(\"td\")\n",
    "        date = str(td[0].find(\"a\").get_text()).strip()\n",
    "        list_date.append(date)\n",
    "\n",
    "        open_value = str(td[1].find(\"div\").get_text()).strip()\n",
    "        list_open_value.append(open_value)\n",
    "\n",
    "        high = str(td[2].find(\"div\").get_text()).strip()\n",
    "        list_high.append(high)\n",
    "\n",
    "        close_value = str(td[3].find(\"div\").get_text()).strip()\n",
    "        list_close_value.append(close_value)\n",
    "\n",
    "        low = str(td[4].find(\"div\").get_text()).strip()\n",
    "        list_low.append(low)\n",
    "\n",
    "        volume_num = str(td[5].find(\"div\").get_text()).strip()\n",
    "        list_volume_num.append(list_volume_num)\n",
    "\n",
    "        volume = str(td[6].find(\"div\").get_text()).strip()\n",
    "        list_volume.append(volume)\n",
    "\n",
    "        print(str(td[0].find(\"a\").get_text()).strip(), end=\" \")  # 日期\n",
    "        print(str(td[1].find(\"div\").get_text()).strip(), end=\" \")  # 开盘价\n",
    "        print(str(td[2].find(\"div\").get_text()).strip(), end=\" \")  # 最高价\n",
    "        print(str(td[3].find(\"div\").get_text()).strip(), end=\" \")  # 收盘价\n",
    "        print(str(td[4].find(\"div\").get_text()).strip(), end=\" \")  # 最低价\n",
    "        print(str(td[5].find(\"div\").get_text()).strip(), end=\"\")  # 交易量\n",
    "        print(str(td[6].find(\"div\").get_text()).strip())  # 交易金额\n",
    "\n",
    "    dict_table = {\"date\": list_date, \"open\": list_open_value,\n",
    "                  'close': list_close_value,\n",
    "                  'high': list_high, 'low': list_low,\n",
    "                  'volume': list_volume, 'volume_num': list_volume_num\n",
    "                  }\n",
    "    print(\"Done\")\n",
    "    print(dict_table)\n",
    "    data = pd.DataFrame(dict_table, columns=columns)\n",
    "#     data.date = pd.to_datetime(data.date)\n",
    "    # data.date = data.date.apply(lambda x: date2num(x))\n",
    "    print(data.head())\n",
    "\n",
    "else:\n",
    "    print(\"该股票或日期不存在\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
